package main

import (
	"context"
	"fmt"
	"gitee.com/sillyman/mixlog"
	"os"
	"os/exec"
	"os/signal"
	"path/filepath"
	"runtime"
	"syscall"
	"time"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())

	list := vlcStartTransmit(ctx)
	if len(list) == 0 {
		mixlog.Errorf("没有播放任何视频流，程序将退出。")
		os.Exit(1)
	}

	fmt.Println("\n作者：岳阳小罗")
	fmt.Println("\n注意：请使用 VLC v3.0 或更新的版本播放以下视频流：")
	for i, uri := range list {
		fmt.Printf("\t[%d] %s\n", i, uri)
	}
	fmt.Println("\n如需退出，请按 `CTRL+C`……")

	//创建监听退出
	//监听指定信号 ctrl+c kill
	sigCh := make(chan os.Signal)
	signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		for s := range sigCh {
			switch s {
			case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
				mixlog.Infof("收到一个 %s 信号，程序即将退出...", s.String())
				exitFunc(cancel)
				return
			default:
			}
		}
	}()

	<-ctx.Done()

	time.Sleep(time.Second)
	os.Exit(0)
}

//exitFunc 退出清理
func exitFunc(cancel context.CancelFunc) {
	// TODO 修复win10的exec问题后，应该删除此
	if runtime.GOOS == `windows` {
		cmd := exec.Command("taskkill.exe", "/f", "/im", filepath.Base(Conf.VLCBin))
		_ = cmd.Run()
	}

	cancel()
}
